package com.frxuexi.park2304.notice

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.ItemNoticeBinding

class NoticeAdapter:PagingDataAdapter<NoticeBeanItem,NoticeAdapter.NoticeViewHolder> (diff){

    private var mOnItemClickListener:OnItemClickListener?=null
    private var mOnItemLongClickListener:OnItemLongClickListener?=null
    companion object{
        val diff=object :ItemCallback<NoticeBeanItem>(){
            override fun areItemsTheSame(
                oldItem: NoticeBeanItem,
                newItem: NoticeBeanItem
            ): Boolean {
                return oldItem.id==newItem.id
            }

            override fun areContentsTheSame(
                oldItem: NoticeBeanItem,
                newItem: NoticeBeanItem
            ): Boolean {
                return oldItem==newItem
            }
        }
    }


    class NoticeViewHolder(itemView: View):ViewHolder(itemView){
        val binding=DataBindingUtil.bind<ItemNoticeBinding>(itemView)
    }

    override fun onBindViewHolder(holder: NoticeViewHolder, position: Int) {
        val item = getItem(position)
        holder.binding?.bean=item

        holder.itemView.apply {
            setOnClickListener {
                mOnItemClickListener?.onItemClick(position)
            }

            setOnLongClickListener {
                mOnItemLongClickListener?.onItemLongClick(item!!)!!
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoticeViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_notice, null)
        return NoticeViewHolder(inflate)
    }

    fun setOnItemClickListener(listener: OnItemClickListener){
        this.mOnItemClickListener=listener
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener){
        this.mOnItemLongClickListener=listener
    }
}

interface OnItemClickListener{
    fun onItemClick(position: Int)
}

interface OnItemLongClickListener{
    fun onItemLongClick(item: NoticeBeanItem):Boolean
}
